﻿//---------------------------------------类型节点-------------------
//@思恒    创建开始节点
function createStartNode() {
    createNode("../Images/start.png", "actStart", 72, 72);
}

//@思恒 创建活动节点
function createActNode() {
    createNode("../Images/act.png", "typeCommon", 100, 70);
}

//@思恒 创建条件分支节点
function createOrSplitNode() {
    createNode("../Images/orSplit.png", "routeOrSplit", 72, 72);
}

//@思恒 创建并行分支节点
function createAndSplitNode() {
    createNode("../Images/andSplit.png", "routeAndSplit", 72, 72);
}

//@思恒 创建异或分支节点
function createXorSplitNode() {
    createNode("../Images/xorSplit.png", "routeXorSplit", 72, 72);
}

//@思恒 创建结束节点
function createEndNode() {
    createNode("../Images/end.png", "actEnd", 72, 72);
}

//@思恒 创建条件合并节点
function createOrJoinNode() {
    createNode("../Images/orJoin.png", "routeOrJoin", 72, 72);
}

//@思恒    创建并行合并节点
function createAndJoinNode() {
    createNode("../Images/andJoin.png", "routeAndJoin", 72, 72);
}

//@思恒    创建异或合并节点
function createXorJoinNode() {
    createNode("../Images/xorJoin.png", "routeXorJoin", 72, 72);
}

//@思恒    创建回退节点
function createRollbackNode() {
    createNode("../Images/rollback.png", "actRollback", 72, 72);
}

//--------------------------节点操作-------------------------------------
//@思恒 增加活动id的系数
function actIdNum() {
    if (arguments.length < 1) return parseInt(document.querySelector("#xmlarea").getAttribute("idum"));
    $id("xmlarea").setAttribute("idum", arguments[0]);
    return -1;
}

//@思恒 创建活动节点
function createNode(img, actType, width, heigth) {
    var lastActIdNum = actIdNum();
    createCanvasNode(img, actType, width, heigth, lastActIdNum);
    createXmlNode(actType, lastActIdNum);
    actIdNum( ++lastActIdNum);
}

//@思恒 创建canvas节点
function createCanvasNode(img, actType, width, heigth,idnum) {
   
    var act = document.createElement("divwf");
    act.setAttribute("id", prefixDivActId + idnum);
    act.className = "draggable";
    act.className += " block actname ";// set class
    
    //--------------设置act的样式------------------------------------------
    act.style.left = "10px";
    act.style.top = "10px";
    act.style.height = heigth + "px";
    act.style.width = width + "px";
    act.style.background = "url(" + img + ")";
    //-------end----设置act的样式------------------------------------------
    
    act.onmousedown = startDrag;
    act.onmouseup = stopDrag;
    act.onclick = function() {
        mageMttrs(act);
    };
    $id("mainCanvas_innerDiv").appendChild(act);

    //------------动态添加到画布------------
    var canvas = findCanvas('mainCanvas');
    var newBlock = new Block(act, canvas);
    newBlock.initBlock();
    canvas.blocks.push(newBlock);
    //------------动态添加到画布--end------
}

//@思恒 创建可拖动图像时 创建一个对应的xml节点
function createXmlNode(actType, idnum) {
    var act = document.createElement(tagwfact);
    act.setAttribute(attrId, prefixWfActId + idnum);
    act.setAttribute(attrName, "新活动");
    act.setAttribute(attrType, actType);

    if (actType == typeRouteOrSplit) {
        act.setAttribute(attrIn, "Y");
    } else {
        act.setAttribute(attrIn, "N");
    }
    act.setAttribute(attrOut, "N");// default output is false;

    act.innerHTML = "<wfdatabody><wfModule><wfmodid></wfmodid><wfurl></wfurl><wfdesc></wfdesc></wfModule><wfusers></wfusers></wfdatabody>";
    $id("xmlarea").appendChild(act);
}

//@思恒 删除活动
function deleteAct() {
    /*
     *删除活动能时 
     1.先删除后置路径和后置节点对应的前置路径 
     2.删除前置路径和前置节点对应的后续路径
     3.删除该节点
     4.删除html元素
    */
    var act = $id($id("id_ipt_attr_id").value);
    if (!act) {
        alert("选择节点!");
        return;
    }
    if (!confirm("确定要删除?删除将无法恢复!")) return;

    //1.删除前置节点为该节点的前置路径
    var paths = $id("xmlarea").querySelectorAll(tagwfpreact + "[" + attrPreActId + "=" + act.id + "]");
    var i;
    if (paths) {
        for (i = 0; i < paths.length; i++) {
            paths[i].parentNode.removeChild(paths[i]);
        }
    }
    //2. 删除后续节为该活动的路径
    paths = $id("xmlarea").querySelectorAll(tagwfnextact + "[" + attrNextActId + "=" + act.id + "]");
    if (paths) {
        for (i = 0; i < paths.length; i++) {
            paths[i].parentNode.removeChild(paths[i]);
        }
    }

    //3. 删除html中的元素

    //3.1 删除html中后续路径
    paths = act.querySelectorAll(tagwfnextact);
    if (paths) {
        for (i = 0; i < paths.length; i++) {
            del(act.id, paths[i].getAttribute(attrNextActId), false);
        }
    }
    //3.2 删除html中前置路径
    paths = act.querySelectorAll(tagwfpreact);
    if (paths) {
        for (i = 0; i < paths.length; i++) {
            del(paths[i].getAttribute(attrPreActId), act.id, false);
        }
    }
    //3.3 删除html中点
    var tempNode = $id(act.id.replace(prefixWfActId, prefixDivActId));
    if (tempNode) tempNode.parentNode.removeChild(tempNode);
    
    //4. 删除xml中节点
    act.parentNode.removeChild(act);
}

//--------------路径----------------------------------------------------------
//构建路径选项列表(后续活动列表)
function clickthis(item) {
    $id("id_ipt_temp_id").value = item.id;
    $id("id_ipt_temp_name").value = item.innerHTML;
}

function removeLi(ul) {
    var uls = $id(ul);
    if (uls == undefined || uls.length == 0) return;

    if ($id("id_ipt_temp_id").value == "") return;
    var count = uls.children.length;
    for (var i = 0; i < count; i++) {
        if (uls.children[i].firstChild.id == $id("id_ipt_temp_id").value) {
            uls.removeChild(uls.children[i]);
            break;
        }
    }
}

function addLi(ul) {
    var uls = $id(ul);
    if (uls == null || !uls) return;
    var v = document.createElement("li");
    var id = $id("id_ipt_temp_id").value;
    if (id == "") return;

    var a = document.createElement("a");
    a.id = id;
    a.innerHTML = $id("id_ipt_temp_name").value;
    a.setAttribute("onclick", "clickthis(this)");
    a.onclick = function () {
        clickthis(a);
    };
    v.appendChild(a);
    uls.appendChild(v);
}

function clearTemp() {
    $id("id_ipt_temp_id").value = "";
    $id("id_ipt_temp_name").value = "";
}

function addToRight() {
    addLi("id_ul_right");
    removeLi("id_ul_left");
    clearTemp();
}

function addAllToRight() {
    moveAll("id_ul_left", "id_ul_right");
    clearTemp();
}

function addToLeft() {
    addLi("id_ul_left");
    removeLi("id_ul_right");
    clearTemp();
}

function moveAll(orign, target) {
    if ($id(orign) == null || $id(target) == null) return;
    var org = $id(orign);
    var tar = $id(target);
    var count = org.children.length;
    if (count == 0) return;
    for (var i = 0; i < count; i++) {
        var temp = org.children[0].cloneNode(true);
        tar.appendChild(temp);
        org.removeChild(org.children[0]);
    }
}

function addAllToLeft() {
    moveAll("id_ul_right", "id_ul_left");
    clearTemp();
}
//---------------------------------------------------------------------------

//@思恒 初始化路径新增列表
function addNewPath() {
    var act = $id($id("id_ipt_attr_id").value);
    //结束活动和回退活动没后续活动
    if (!act || act.getAttribute(attrType) == typeActEnd || act.getAttribute(attrType) == typeActRollback) return;

    $id("lb_ident").innerHTML = "PATH";
    $id("id_div_left").innerHTML = "";
    $id("id_ul_right").innerHTML = "";

    //获取活动
    var acts = $more(tagwfact);
    if (acts == null || acts.length == 0) return;
    var count = acts.length;

    //构造列表
    var htms = "<ul id='id_ul_left' >";
    var preActs = act.querySelectorAll(tagwfpreact);
    var preCount = preActs == null ? 0 : preActs.length;
    var nextActs = act.querySelectorAll(tagwfnextact);
    var nextCount = nextActs == null ? 0 : nextActs.length;


    for (var i = 0; i < count; i++) {
        if (act.id == acts[i].id || acts[i].getAttribute(attrType) == typeActStart) continue;
        var ident = false;

        //如果活动是该活动的前置活动
        var j;
        for (j = 0; j < preCount; j++) {
            if (preActs[j].getAttribute(attrPreActId) == acts[i].id) {
                ident = true;
                break;
            }
        }
        if (ident) continue;
        //如果活动是该活动的后续活动
        for (j = 0; j < nextCount; j++) {
            if (nextActs[j].getAttribute(attrNextActId) == acts[i].id) {
                ident = true;
                break;
            }
        }
        if (ident) continue;

        htms += "<li><a  id=\"_" + acts[i].id + "\" onclick=\"clickthis(this)\"> " + acts[i].getAttribute(attrName) + "</a></li>";

    }
    htms += "</ul>";
    $id("id_div_left").innerHTML = htms;

    $id("div_float_container").style.display = "block";
}

//@思恒 当点击确定时修改路径或增加路径
function ModifyPath() {
    var uls = $id("id_ul_right");
    var count = uls.children.length;
    var act = $id($id("id_ipt_attr_id").value);

    if (count == 0) {
        $id("div_float_container").style.display = "none";
        return;
    }

    for (var i = 0; i < count; i++) {
        var nextAct = $id("id_ul_right").children[i].children[0];
        if (!nextAct) continue;


        //1 添加信息到列表
        var tb = $id("id_tb_attr_acts").children[0]; //获取tbody
        var newRow = tb.insertRow(1); // 插入一行
        var cel = newRow.insertCell(0);
        cel.innerHTML = act.id;
        cel.style.display = "none";

        var tdTemp = newRow.insertCell(1);
        tdTemp.innerHTML = nextAct.innerHTML;
        tdTemp.style.width = "85px";

        var tempNextID = nextAct.id.substring(1, nextAct.id.length);
        var tempType = $id(tempNextID).getAttribute(attrType);
        if (tempType == typeRouteOrSplit) {
            act.setAttribute(attrIn, varlueTrue);

            tdTemp = newRow.insertCell(2);
            tdTemp.innerHTML = " <label onclick=\"showConditionEditor(this)\" style='width:100px;'>点击编辑条件</label>";
        } else {
            tdTemp = newRow.insertCell(2);
            tdTemp.innerHTML = "";
        }

        tdTemp.style.width = "95px";
        tdTemp = newRow.insertCell(3);
        tdTemp.innerHTML = "<label onclick=\"deletePath(this)\">X</label>";
        tdTemp.style.width = "18px";

        //2.添加连线
        var wfpath = document.createElement("div");
        wfpath.id = prefixWfPath + act.id + tempNextID;
        wfpath.title = "path";
        wfpath.className = "connector";
        wfpath.className += " " + act.id.replace(prefixWfActId, prefixDivActId);
        wfpath.className += " " + tempNextID.replace(prefixWfActId, prefixDivActId);
        wfpath.innerHTML = "<img src=\"../Images/arrow.gif\" class=\"connector-end\" />";
        //如果是路由活动,则加上说明(label)
        if (act.getAttribute(attrType) == typeRouteOrSplit) {
            wfpath.innerHTML += " <label class='middle-label' id='" + prefixwfLabId + act.id + tempNextID + "'></label>";
        }

        $id("mainCanvas_innerDiv").appendChild(wfpath);

        var canvas = findCanvas('mainCanvas');
        var newConnection = new Connector(wfpath, canvas);
        newConnection.initConnector();
        canvas.connectors.push(newConnection);
        $id("div_float_container").style.display = "none";

        //------------添加xml path------------------------------------
        //-------------添加活动的后续活动能
        var path = document.createElement(tagwfnextact);
        path.setAttribute(attrNextActId, tempNextID);
        act.firstChild.appendChild(path);
        //-------------添加活动的前置活动
        path = document.createElement(tagwfpreact);
        path.setAttribute(attrPreActId, act.id);
        $id(tempNextID).firstChild.appendChild(path);
        //------------添加xml path-----------end----------------------
    }
}

//@思恒 删除路径
function deletePath(lbDel) {
    var nextWfActId = lbDel.parentNode.parentNode.firstChild.innerHTML;
    var actId = $id("id_ipt_attr_id").value;
    del(actId, nextWfActId, false);//为true执行一次而不检查 
}

//@思恒 删除指定路径
function del(preActId, nextWfActId, isTerminated) {
    var nextActId = nextWfActId;
    var actid = preActId;

    //1.删除页面里面路径
    var pathIdPrefix = prefixWfPath + actid + nextActId;
    var i;
    for (i = 1; i <= 3; i++) {
        var path = $id(pathIdPrefix + "_" + i);
        if (path) path.parentNode.removeChild(path);
    }
    //2. 删除路径图片
    var img = $single("img[" + attrWfpath + "=" + pathIdPrefix + "]");
    if (img) img.parentNode.removeChild(img);

    //3. 删除路径上的说明
    var lb = $id(prefixwfLabId + actid + nextActId);
    if (lb) lb.parentNode.removeChild(lb);

    //-------删除canvas里面数据
    var canvas = findCanvas('mainCanvas');
    var count = canvas.connectors.length;
    for (i = 0; i < count; i++) {
        if (canvas.connectors[i].id == pathIdPrefix) {
            canvas.connectors.splice(i, 1);
            break;
        }
    }

    //5. 删除$id("xmlarea")中相关的条件数据
    // 删除当前wfact中的wfnextact 和后续wfact的wfpreact
    var act = $id(actid).querySelector(tagwfnextact + "[" + attrNextActId + "=" + nextActId + "]");
    if (act) act.parentNode.removeChild(act);
    act = $id(nextActId).querySelector(tagwfpreact + "[" + attrPreActId + "=" + actid + "]");
    if (act) act.parentNode.removeChild(act);

    if (isTerminated) return;
    //再进行检查一次
    del(preActId, nextWfActId, true);//只能为true,否则会陷入死循环之中
}
//----------------------------------------------------------------------